<?php
/**
 *
 */

abstract class RPHP_Db_Abstract
{

    protected $_config = array();
    protected $_connection;
    protected $_query = null;
    protected $_log   = array();
    protected $_lastSql;
    protected $error_reporting = true;

    public function __construct($config)
    {
        $keys = array('host', 'port', 'user', 'password', 'database', 'prefix', 'charset', 'autocommit');
        foreach ($keys as $key) {
            if (isset($config[$key])) {
                $this->_config[$key] = $config[$key];
            }
        }

        if (null === $this->_connection) {
            $this->_connection = $this->_connect($this->_config);
        }
    }

    public function sql($sql, $type = 'ASSOC')
    {

        $o = $this->query($sql);
        if ($o) {
            $tags = explode(' ', $sql, 2);

            switch (strtoupper($tags[0])) {
                case 'SELECT':
                    ($result = $this->fetchAll($type)) || ($result = array());
                    break;
                case 'INSERT':
                    $result = $this->lastInsertId();
                    break;
                case 'UPDATE':
                case 'DELETE':
                    $result = $this->affectedRows();
                    break;
                default:
                    $result = $this->_query;
            }

            return $result;
        }

    }

    abstract protected function _connect($params);

    abstract public function error($type = 'STRING');

    abstract public function close();

    abstract public function query($sql);

    abstract public function getrow($sql);

    abstract public function getone($sql);

    abstract public function getnum($sql);

    abstract public function affectedRows();

    abstract public function fetch();

    abstract public function fetchAll();

    abstract public function lastInsertId();

}
